
package com.zbest.component.framework.pigeon.config.spring.schema;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import com.zbest.component.framework.pigeon.extension.ExtensionBean;
import com.zbest.component.framework.pigeon.template.TemplateBean;

/**
 * 
 * 
 * @author jianhuihuang
 * @version $Id: PigeonBeanDefinitionParser.java, v 0.1 2013-6-24 下午9:58:37
 *          jianhuihuang Exp $
 */
public class PigeonBeanDefinitionParser implements BeanDefinitionParser {

	/** Default placeholder prefix: "${" */
	public static final String DEFAULT_PLACEHOLDER_PREFIX = "${";
	/** Default placeholder suffix: "}" */
	public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";

	// private static final Logger logger =
	// Logger.getLogger(PigeonBeanDefinitionParser.class);

	private final Class<?> beanClass;

	private final boolean required;

	public PigeonBeanDefinitionParser(Class<?> beanClass, boolean required) {
		this.beanClass = beanClass;
		this.required = required;
	}

	public BeanDefinition parse(Element element, ParserContext parserContext) {
		return parse(element, parserContext, beanClass, required);
	}

	private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) {
		RootBeanDefinition beanDefinition = new RootBeanDefinition();
		// beanDefinition.setBeanClass(beanClass);
		beanDefinition.setLazyInit(false);
		String id = element.getAttribute("id");

		if (beanClass == ExtensionBean.class) {
			beanDefinition.setBeanClass(ExtensionBean.class);
			beanDefinition.setInitMethodName("init");

			MutablePropertyValues properties = beanDefinition.getPropertyValues();
			properties.addPropertyValue("point", element.getAttribute("point"));
			String bean = element.getAttribute("bean");
			if (StringUtils.isEmpty(bean) || !parserContext.getRegistry().containsBeanDefinition(bean)) {
				throw new IllegalStateException("template must have a reference to bean");
			}
			properties.addPropertyValue("bean", new RuntimeBeanReference(bean));

			Element contentElement = DomUtils.getChildElementByTagName(element, "content");
			properties.addPropertyValue("content", contentElement);
		}

		else if (beanClass == TemplateBean.class) {
			beanDefinition.setBeanClass(TemplateBean.class);
			beanDefinition.setInitMethodName("init");

			MutablePropertyValues properties = beanDefinition.getPropertyValues();
			properties.addPropertyValue("type", element.getAttribute("type"));
			String templateManager = element.getAttribute("templateManager");
			/*if (StringUtils.isEmpty(templateManager) || !parserContext.getRegistry().containsBeanDefinition(templateManager)) {
				throw new IllegalStateException("templateManager must have a reference to bean");
			}*/
			properties.addPropertyValue("templateManager", new RuntimeBeanReference(templateManager));

			Element contentElement = DomUtils.getChildElementByTagName(element, "content");
			properties.addPropertyValue("content", contentElement);

		}

		parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);

		return beanDefinition;
	}

}